
const yDb = require('../../model/yunshijie');
const User = yDb.User;
const Promise = require('bluebird');

/**
 * 收藏快讯
 * @param par
 * {
 *      uid: 用户Id
 *      fastId: 快讯Id
 * }
 */
function actFast(par) {

    let uid = par.uid,  //用户Id
        update = par.update,    //修改内容
        fastId = par.fastId,
        type = par.updateField;
    let where = {uid: uid};
    let isUpdate = false;

    if (type === 'collectCount') {
        where['collect._id'] = fastId;
        isUpdate = true;
    }
    if (type === 'topCount') {
        where['vote._id'] = fastId;
        isUpdate = true;
    }

    return User.findOne(where)
        .exec()
        .then((data)=> {
            //如果已经收藏了 就不需要重复收藏了
            if (data && isUpdate) {
                return Promise.resolve()
            }
            return User.update(
                {uid},
                update,
                {upsert: true}
            ).exec()
        })
}

/**
 * 用户的收藏列表
 * @param par
 * {
 *  uid: 用户Id
 * }
 */
function collectList(par) {

    let uid = par.uid;

    return User.findOne({uid}, null, {lean: true}).exec()
}

module.exports = {
    //  收藏快讯
    actFast: actFast,
    //  查询用户的收藏Id
    collectList: collectList
};